"""
Case Type   : resource pool
Case Name   : 创建组资源池后删除关联的子class控制组
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置子class控制组及Workload控制组
    2.创建组资源池关联class_a控制组
    3.创建用户关联组资源池
    4.删除控制组class_a
    5.查询组资源池信息和用户信息
    6.切换到创建的用户，执行业务
    7.环境清理
Expect      :
    1.创建成功
    2.创建成功
    3.创建成功
    4.成功
    5.成功
    6.执行成功
    7.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_a = 'class_a'
        self.class_b = 'class_b'
        self.group_a = 'workload_a1'
        self.group_b = 'workload_b1'
        self.respool = 'resp_resource_pool_0062'
        self.user = 'u_resource_pool_0062'

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_a} -s 20; " \
              f"gs_cgroup -c -S {self.class_a} -G {self.group_a} -g 20; " \
              f"gs_cgroup -c -S {self.class_b} -s 10; " \
              f"gs_cgroup -c -S {self.class_b} -G {self.group_b} -g 60; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_a}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"CLASS  Name: {self.class_b}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_a}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_b}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建组资源池关联class_a控制组;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool} "
            f"with(control_group = '{self.class_a}');")
        self.log.info(sql_res)
        self.assertEqual(self.constant.create_resource_pool_success, sql_res,
                         f'执行失败:{text}')

        text = "-----step3:创建用户关联组资源池;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"drop user if exists {self.user} cascade;"
            f"create user {self.user} resource pool '{self.respool}' "
            f"password '{macro.COMMON_PASSWD}';")
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res,
                      f'执行失败:{text}')

        text = "-----step4:删除控制组class_a;expect:成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -d -S {self.class_a}; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertNotIn(f"CLASS  Name: {self.class_a}", cmd_res,
                         f'执行失败:{text}')
        self.assertNotIn(f"DEFWD  Name: {self.group_a}", cmd_res,
                         f'执行失败:{text}')

        text = "-----step5:查询组资源池信息和用户信息;expect:成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"select * from pg_resource_pool;"
            f"select usename, respool from pg_user "
            f"where usename = '{self.user}';")
        self.log.info(sql_res)
        self.assertIn(f'{self.respool} |          20 |           -1 '
                      f'| {self.class_a}', sql_res, f'执行失败:{text}')
        self.assertIn(f"{self.user} | {self.respool}", sql_res,
                      f'执行失败:{text}')

        text = "-----step6:切换到创建的用户，执行业务;expect:成功-----"
        self.log.info(text)
        sql = f'''drop table if exists t_resource_pool_0062 cascade;
        create table t_resource_pool_0062(c_int int primary key, 
          c_vchar varchar(20), c_bigint bigint, c_date date);
        insert into t_resource_pool_0062 values(generate_series(1,1000000), 
          'test'|| generate_series(1,1000000), 12345698, date'2022-6-4');
        analyze t_resource_pool_0062;
        create index i_resource_pool_0062 on t_resource_pool_0062(c_vchar);
        select count(*) from t_resource_pool_0062 where c_int>5000;
        truncate table t_resource_pool_0062;
        vacuum t_resource_pool_0062;
        drop table if exists t_resource_pool_0062 cascade;'''
        sql_res = self.pri_sh.execut_db_sql(
            sql, sql_type=f'-U {self.user} -W{macro.COMMON_PASSWD}')
        self.log.info(sql_res)
        self.assertIn(self.constant.ANALYZE_SUCCESS_MSG, sql_res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS, sql_res,
                      f'执行失败:{text}')
        self.assertIn('995000\n(1 row)', sql_res, f'执行失败:{text}')
        self.assertIn(self.constant.TRUNCATE_SUCCESS_MSG, sql_res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, sql_res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS,
                      sql_res.splitlines()[-1], f'执行失败:{text}')

    def tearDown(self):
        text = "-----step7:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop user if exists {self.user} cascade;"
            f"drop resource pool if exists {self.respool};")
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.drop_resource_pool_success, drop_res,
                      f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
